<template>
  <div class="card amap-example">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules
      label-width="auto"
      style="max-width: 600px"
       :validate-on-rule-change="false"
    >
      <el-form-item label="Activity name" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>
      <el-form-item label="地址" required>
        <!-- 1. 使用组件 -->
        <AmapSearch />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="ruleForm.remark" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          Create
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts" name="AmapFromSearch">
import { reactive, ref, provide } from 'vue'
import type { ElForm, FormRules } from 'element-plus'
import AmapSearch from '@/components/AmapSearch/index.vue'

interface RuleForm {
  name: string
  remark: string
}
type FormInstance = InstanceType<typeof ElForm>
const ruleFormRef = ref<FormInstance>()
let ruleForm = reactive<RuleForm>({
  name: '',
  remark: '',
})

let rules = reactive<FormRules<RuleForm>>({
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  remark: [{ required: true, message: '请输入备注', trigger: 'blur' }],
})

// 2. 传入formData和formRules
provide('aMap', { ruleForm, rules })

const submitForm = async (formEl: FormInstance | undefined) => {
  console.log(ruleForm, 's')
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<style lang="scss" scoped>
.amap-example {
  display: flex;
  width: 100%;
  height: 100%;
}
</style>
